package com.mes.controller.warehouse;

import com.interf.WCSUtils;
import com.mes.service.manu.FlyWeavService;
import com.mes.service.warehouse.XbkSjListService;
import com.interf.agv.AGVUtil;
import com.yy.base.page.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 线边库上架
 */
@Controller
@RequestMapping("xbkSjList")
public class XbkSjListController {
    @Autowired
    private XbkSjListService xbkSjListService;
    @Autowired
    private FlyWeavService flyWeavService;

    //通过RFID工单号加载工单
    @ResponseBody
    @RequestMapping(value="selectXbkSjListByCode",method= RequestMethod.POST)
    public Message selectXbkSjListByCode(HttpServletRequest request){
        Message msg = new Message();
        String rfid = request.getParameter("code");   //RFID
        String code = rfid.replaceAll(" ", "");

        //区分传过来的code是物料号还是工单号
        String type = "";

        //判断传过来的RFID是否在工单列表或者物料列表中存在
        List<Map<String, Object>> ListPlan = xbkSjListService.selectXbkPlanListByCode(code); //传过来的是作业工单号

        //  List<Map<String, Object>> ListProduct = xbkSjListService.selectXbkProductListByCode(code); //传过来的是物料号
        List<Map<String, Object>> ListProduct = xbkSjListService.getBatchnorLists(code); //传过来的是RFID号

        if(ListPlan.size() == 0 && ListProduct.size() == 0 ){
            msg.setData("xbkSjListByCode","false");
            return msg;
        }

        if(ListProduct.size() != 0){
            type = "WL";
            msg.setData("PlanList",ListProduct);
        }else if(ListPlan.size() != 0){
            type = "PLAN";
            msg.setData("PlanList",ListPlan);
        }
        return msg;
    }

    //通过RFID工单号及数量存入中间表
    @ResponseBody
    @RequestMapping(value="saveXbkSjListByCode",method= RequestMethod.POST)
    public Message saveXbkSjListByCode(HttpServletRequest request){
        Message msg = new Message();
        try{

            String rfid = request.getParameter("code").toString();   //RFID
            String qtys = request.getParameter("qtys").toString();   //上架数量
            String code = rfid.replaceAll(" ", "");

            int numbers = Integer.parseInt(qtys);

            //区分传过来的code是物料号还是工单号
            String type = "";

            //判断传过来的RFID是否在工单列表或者物料列表中存在
            List<Map<String, Object>> ListPlan = xbkSjListService.selectXbkPlanListByCode(code); //传过来的是作业工单号

          //  List<Map<String, Object>> ListProduct = xbkSjListService.selectXbkProductListByCode(code); //传过来的是物料号
            List<Map<String, Object>> ListProduct = xbkSjListService.getBatchnorLists(code); //传过来的是RFID号

            if(ListPlan.size() == 0 && ListProduct.size() == 0 ){
                msg.setData("xbkSjListByCode","false");
                return msg;
            }

            if(ListProduct.size() != 0){
                type = "WL";
            }else if(ListPlan.size() != 0){
                type = "PLAN";
            }
            List<Map<String, Object>> mescode = null;
            String ckCode = "";
            if(type == "PLAN"){
                mescode = xbkSjListService.selectMesParameter("PATH1909090004"); //查询鞋面辅料MES参数

                ckCode = mescode.get(0).get("CSVALUE").toString();  //MES参数编码（仓库编码）

                //判读该作业工单是否已经上传至中间表
                int num  = WCSUtils.check(ckCode, code);
                //设置AGV状态为空闲
                setAgvEnd(code);

                //等于0代表未上传,则插入上架数据,不等于0则代表已上传，插入下架数量
                if(num == 0) {
                    //调用接口往中间表插数据
                    WCSUtils.inStorck(ckCode, code, code, code, code + "物料", numbers);
                } else {

                    //调用接口往中间表插数据
                    WCSUtils.outStorck(ckCode, code, numbers, code);
                }
            }else if(type == "WL"){

                String WLVALUE = ListProduct.get(0).get("WLVALUE").toString();  //物料编码
                String WLNAME = ListProduct.get(0).get("WLNAME").toString();  //物料名称
                String WLID = ListProduct.get(0).get("WLID").toString();  //物料ID


                mescode = xbkSjListService.selectWarehouseCode(WLID);

                ckCode = mescode.get(0).get("CKCODE").toString();  //MES参数编码（仓库编码）
                //调用接口往中间表插数据
                WCSUtils.inStorck(ckCode, code, code, WLVALUE, WLNAME, numbers);
            }
        }catch (Exception e){
            msg.setData("result","false");
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 标记agv接口取鞋面完成
     */
    private void setAgvEnd(String code) {
        //鞋面状态为获取中的时候，则将AGV的状态设置为空闲
        if ("GET".equalsIgnoreCase(flyWeavService.getXmStatus(code))) {
            //修改鞋面状态为完成
            flyWeavService.updateXmStatus(code, "OK");

            //设置AGV的状态为空闲
            String jsonStr = "{\"task_seq\":\"" + code + "\",\"submd\":5,\"flyloom_id\":3," +
                    "\"dest_position\":{\"x\":0,\"y\":0}" +
                    "}";
            AGVUtil.sendAGVTask(jsonStr, 0x00060005);
        }
    }
}
